﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyBlog2.Models
{
    public class DbInitializer
    {
        public static void seed(AppDbContext context)
        {
            //添加用户
            if (context.Users.Any())  //判断是否有数据
            {
                return;
            }
            context.Users.Add(
                    new User { Id = 1, UserName = "tingyu", Password = "2eb2bc3caafd7a73e12d2d7107f22b76", Nickname = "*听雨*",Email = "1186472484@qq.com" }
                );
            context.SaveChanges();


            //添加文章分类
            if (context.ArticleClassifys.Any())  //判断是否有数据
            {
                return;
            }
            context.ArticleClassifys.AddRange(
                    new ArticleClassify { Id = 1, Name = "C#", Sort = 1},
                    new ArticleClassify { Id = 2, Name = "Linux", Sort = 2 },
                    new ArticleClassify { Id = 3, Name = "java", Sort = 1 },
                    new ArticleClassify { Id = 4, Name = "python", Sort = 1 },
                    new ArticleClassify { Id = 5, Name = "php", Sort = 1 }
                );
            context.SaveChanges();

            //添加文章标签
            if (context.Tags.Any())  //判断是否有数据
            {
                return;
            }
            context.Tags.AddRange(
                    new Tag { Id = 1, Name = "winform" },
                    new Tag { Id = 2, Name = "asp .net core" },
                    new Tag { Id = 3, Name = "ubuntu" },
                    new Tag { Id = 4, Name = "elementary os" },
                    new Tag { Id = 5, Name = "manjaro" },
                    new Tag { Id = 6, Name = "arco linux" }
                );
            context.SaveChanges();



            //添加文章
            if (context.Articles.Any())  //判断是否有数据
            {
                return;
            }
            context.Articles.AddRange(
                    new Article { Id = 1, ArticleClassifyId = 1, Title = "winform界面美化", Description = "winform界面美化aa", Contents = "winform界面美化bbb", Flag = "原创", Views = 0, CreateTime = DateTime.Now, UpdateTime = DateTime.Now, Published = true, TagIds = "1,2,3," },
                    new Article { Id = 2, ArticleClassifyId = 2, Title = "manjaro美化", Description = "manjaro美化美化aa", Contents = "manjaro美化bbb", Flag = "原创", Views = 0, CreateTime = DateTime.Now, UpdateTime = DateTime.Now, Published = true, TagIds = "1,2,3," }
                );
            context.SaveChanges();


            //添加评论
            if (context.ArticleComments.Any())  //判断是否有数据
            {
                return;
            }
            context.ArticleComments.AddRange(
                    new Comment { Id = 1, ArticleId = 1, Nickname = "张三", Email = "zhangsan@163.com", Contents = "不错", AddTime = DateTime.Now ,AdminComment = false},
                    new Comment { Id = 2, ArticleId = 2, Nickname = "张三", Email = "zhangsan@163.com", Contents = "很好有用", AddTime = DateTime.Now, AdminComment = false }
                );
            context.SaveChanges();
        }
    }
}
